from django.conf import settings
from django.core.mail import send_mail
from django.template.loader import get_template
from django.urls import reverse
from celery import shared_task


@shared_task
def send_active_mail(username, recipient_list, token):
    """发送激活邮件"""
    subject = f'{settings.SITE_HOST}激活'
    link = f"http://{settings.SITE_HOST}{reverse('account:activate_user_account', args=(token,))}"
    template = get_template('activation_email.txt')
    context = {
        'username': username,
        'link': link,
        'site_name': settings.SITE_NAME
    }
    message = template.render(context)
    send_mail(
        subject=subject,
        message=message,
        from_email=settings.DEFAULT_FROM_EMAIL,
        recipient_list=recipient_list
    )